package com.mstore.dao;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.mstore.pojo.Member;

public class MemberDao extends SuperDao {

	public void add(Member member) {
		super.save(member);
	}

	public void update(Member member) {
		super.update(member);
	}

	/**
	 * 
	 * 通过type查询企业下的会员
	 */
	public List<Object> getAllMember(Member member) {
		String sql = "from Member where eaId=? and type =?";
		return super.getObject(sql,
				new Object[] { member.getEaId(), member.getType() });
	}

	/**
	 * 
	 * 分页
	 */
	public List<Object> getMemberPage(int index, Member member) {
		/*
		 * String hql = "from Member where eaId=" + member.getEaId() +
		 * "order by createTime desc"; return super.findPage(index, hql);
		 */
		StringBuilder sb = new StringBuilder();
		sb.append("from Member where eaId=");
		sb.append(member.getEaId());
		sb.append("order by createTime desc");
		return super.findPage(index, sb.toString());
	}

	/**
	 * 
	 * 全部条数
	 */
	public int getMemberLine(Member member) {
		String hql = "from Member where eaId=?";
		List<Object> objects = super.getObject(hql,
				new Object[] { member.getEaId() });
		return objects.size();
	}

	/**
	 * 
	 * 查询关注状态的条数
	 */
	public int getMemberLineBytype(Member member) {
		String hql = "from Member where eaId=? and type=?";
		List<Object> objects = super.getObject(hql,
				new Object[] { member.getEaId(), member.getType() });
		return objects.size();
	}

	public Object getOneMember(Member member) {
		String hql = "from Member where eaId=? and mobile=?";
		List<Object> objects = super.getObject(hql,
				new Object[] { member.getEaId(), member.getMobile() });
		if (objects.size() != 0) {
			return objects.get(0);
		}
		else {
			return null;
		}
	}

	/**
	 * 通过手机号查询一个会员
	 */
	public Object getmemberByMobile(Member member) {
		String hql = "from Member where mobile=?";
		List<Object> objects = super.getObject(hql,
				new Object[] { member.getMobile() });
		if (objects.size() != 0) {
			return objects.get(0);
		}
		return null;
	}

	/**
	 * 通过手机号和type查询一个会员
	 */
	public Object getMemberByMobileAndType(Member member) {
		String hql = "from Member where mobile = ? and type = 1";
		List<Object> objects = super.getObject(hql,
				new Object[] { member.getMobile() });
		if (objects.size() != 0) {
			return objects.get(0);
		}
		return null;
	}

	/**
	 * 验证手机号的唯一性
	 */
	public boolean checkMobile(Member member) {
		String hql = "from Member where mobile = ?";
		List<Object> objects = super.getObject(hql,
				new Object[] { member.getMobile() });
		return objects.size() > 0;
	}

	/**
	 * 验证手机号但排除自己
	 */
	public boolean checkMobile1(Member member) {
		String hql = "from Member where mobile = ? and email != ?";
		List<Object> objects = super.getObject(hql,
				new Object[] { member.getMobile(), member.getEmail() });
		return objects.size() > 0;
	}

	/**
	 * 验证昵称的唯一性
	 */
	public boolean checkNickName(Member member) {
		String hql = "from Member where nickName = ?";
		List<Object> objects = super.getObject(hql,
				new Object[] { member.getNickName() });
		return objects.size() > 0;
	}

	/**
	 * 验证企业手机号的唯一性
	 */
	public boolean checkActMobile(Member member) {
		String hql = "from Member where eaId=? and mobile=?";
		List<Object> objects = super.getObject(hql,
				new Object[] { member.getEaId(), member.getMobile() });
		return objects.size() > 0;
	}

	public Object getOneMemberByBalance(Long id) {
		String hql = "from Member where id=?";
		List<Object> objects = super.getObject(hql, new Object[] { id });
		if (objects.size() != 0) {
			return objects.get(0);
		}
		else {
			return null;
		}
	}

	public Object getMemberById(Member member) {
		String hql = "from Member where id = ?";
		List<Object> objects = super.getObject(hql,
				new Object[] { member.getId() });
		if (objects.size() != 0) {
			return objects.get(0);
		}
		else {
			return null;
		}
	}

	/**
	 * 删除会员
	 */
	public void delMember(Member member) {
		super.delete(member);
	}

	/**
	 * 验证邮箱
	 * 
	 * @return
	 */
	public List<Object> getMemberByEmail(Member member) {
		/*
		 * String hql = "from Member where email='" + member.getEmail() + "'";
		 * return super.find(hql);
		 */
		StringBuilder sb = new StringBuilder();
		sb.append("from Member where email='");
		sb.append(member.getEmail());
		sb.append(" ' ");
		return super.find(sb.toString());
	}

	/**
	 * 验证邮箱但排除自己
	 */
	public List<Object> getMemberByEmail1(Member member) {
		/*
		 * String hql = "from Member where email='" + member.getEmail() +
		 * "' and mobile != '" + member.getMobile() + "'"; return
		 * super.find(hql);
		 */

		StringBuilder sb = new StringBuilder();
		sb.append("from Member where email='");
		sb.append(member.getEmail());
		sb.append("' and mobile != '");
		sb.append(member.getMobile());
		sb.append(" ' ");
		return super.find(sb.toString());
	}

	/**
	 * 通过手机号/邮箱/昵称和密码查询一名会员
	 */
	public Object checkMember(String loginName, String password) {
		String hql = "from Member where (mobile = ? or email = ?) and password = ?";
		List<Object> list = super.getObject(hql, new Object[] { loginName,
				loginName, password });
		return list != null && list.size() > 0 ? list.get(0) : null;
	}

	/**
	 * 更新邮箱
	 * 
	 * @return
	 */
	public void updateEmail(Member member) {
		/*
		 * String sql = "update c_member set  email ='" + member.getEmail() +
		 * "'  where  id=" + member.getId(); super.executeUpdate(sql);
		 */

		StringBuilder sb = new StringBuilder();
		sb.append("update c_member set  email ='");
		sb.append(member.getEmail());
		sb.append("'  where  id=");
		sb.append(member.getId());
		super.executeUpdate(sb.toString());
	}

	/**
	 * 修改密码根据eaid
	 * 
	 * @return
	 */
	public void updataPasswordByEaId(Member member) {
		/*
		 * String sql = "update c_member set  password ='" +
		 * member.getPassword().trim() + "'  where  id=" + member.getId();
		 * super.executeUpdate(sql);
		 */

		StringBuilder sb = new StringBuilder();
		sb.append("update c_member set  password ='");
		sb.append(member.getPassword().trim());
		sb.append("'  where  id=");
		sb.append(member.getId());
		super.executeUpdate(sb.toString());
	}

	/**
	 * 检查电话号码
	 */
	public List<Object> checkMobile(String mobile) {
		/* String hql = "FROM  Member WHERE  mobile='" + mobile + "'"; */

		StringBuilder sb = new StringBuilder();
		sb.append("FROM  Member WHERE  mobile='");
		sb.append(mobile);
		sb.append("'");
		List<Object> objects = super.find(sb.toString());
		return objects;

	}

	/**
	 * 根据邮箱修改密码
	 * 
	 * @return
	 */
	public void updataPasswordByEmail(Member member) {
		/*
		 * String sql = "update c_member set  password ='" +
		 * member.getPassword().trim() + "'  where  email=" + member.getEmail();
		 * super.executeUpdate(sql);
		 */

		StringBuilder sb = new StringBuilder();
		sb.append("Update c_member set  password ='");
		sb.append(member.getPassword().trim());
		sb.append("'  where  email=");
		sb.append(member.getEmail());
		super.executeUpdate(sb.toString());
	}

	/**
	 * 通过邮箱查找
	 * 
	 * @return
	 */
	public List<Object> findByEmail(Member member) {
		/*
		 * String hql = "from   Member  where   email='" + member.getEmail() +
		 * "'  and  type !=1";
		 */

		StringBuilder sb = new StringBuilder();
		sb.append("from   Member  where   email='");
		sb.append(member.getEmail());
		sb.append("'  and  type !=1");
		List<Object> objects = super.find(sb.toString());
		return objects;
	}

	/**
	 * 通过手机查找
	 * 
	 * @return
	 * 
	 */
	public List<Object> findByMobile(Member member) {
		/*
		 * String hql = "from   Member  where   mobile='" + member.getMobile() +
		 * "'  and  type !=1"
		 */;

		StringBuilder sb = new StringBuilder();
		sb.append("from   Member  where   mobile='");
		sb.append(member.getMobile());
		sb.append("'  and  type !=1");
		List<Object> objects = super.find(sb.toString());
		return objects;
	}

	/**
	 * 通过手机查找
	 * 
	 * @return
	 * 
	 */
	public List<Object> findOneByMobile(Member member) {
		StringBuilder sb = new StringBuilder();
		sb.append("from   Member  where   mobile='");
		sb.append(member.getMobile());
		sb.append("'");
		List<Object> objects = super.find(sb.toString());
		return objects;
	}
	
	/**
	 * 条件查询数量，排除自身
	 */
	public int getCountFromMemberByConditionExceptSelf(Member member) {
		String separate = " where ";
		StringBuilder sql = new StringBuilder();
		sql.append("select count(id) from Member");
		
		String mobile = member.getMobile();
		if (StringUtils.isNotBlank(mobile)) {
			sql.append(separate);
			sql.append("mobile = '");
			sql.append(mobile);
			sql.append("'");
			separate = " and ";
		}
		
		String email = member.getEmail();
		if (StringUtils.isNotBlank(email)) {
			sql.append(separate);
			sql.append("email = '");
			sql.append(email);
			sql.append("'");
			separate = " and ";
		}
		
		String nickName = member.getNickName();
		if (StringUtils.isNotBlank(nickName)) {
			sql.append(separate);
			sql.append("nickName = '");
			sql.append(nickName);
			sql.append("'");
			separate = " and ";
		}
		
		sql.append(separate);
		sql.append("id != ");
		sql.append(member.getId());
		
		List<Object> list = find(sql.toString());
		if (list.isEmpty()) {
			return 0;
		}
		
		Long count = (Long) list.get(0);
		return count.intValue();
	}

}
